﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoreData;
using ActionServices;
using Helper;

namespace ZidoniCMS.Areas.Administrator.Controllers
{
    public class EmailTemplateManagementController : BaseController
    {
        public EmailTemplateManagementController(IEmailListActionService _emaillist, IEmailTemplateActionService _emailtemplate)
            :base(_emaillist, _emailtemplate) {
                try
                {
                    if (Permission == null || Permission.Count <= 0)
                    {
                        SetPermission(UserID, GroupID, UserName);
                    }
                    SessionManagement.SetSesionValue("pvalue", Permission.FirstOrDefault(c => c.Module.Equals(Helper.ValueDefine.EMAILTEMPLATE)).Role);
                }
                catch { }
        }
        //
        // GET: /Administrator/EmailTemplate/

        #region Main
        public ActionResult Index()
        {
            return SetUpSecurity();
        }

        public ActionResult _ShowAll()
        {
            var data = EmailTemplateService.GetList();
            return PartialView(data);
        }
        #endregion
        #region CRUD
        public ActionResult AddOrUpdate(int id = 0)
        {
            return CanUpdate((int)id);
        }

        public ActionResult _AddOrUpdate(int id)
        {
            var data = EmailTemplateService.GetByID(id);
            return PartialView(data);
        }

        /// <summary>
        /// Action
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(EmailTemplate model)
        {
            var exist = this.EmailTemplateService.GetOneByLINQ(c => c.Name.Equals(model.Name));
            if (exist != null && model.ID == 0)
            {
                return Content("exist");
            }
            var id = this.EmailTemplateService.Save(model);
            var data = this.EmailTemplateService.GetByID(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Delete(int id)
        {
            this.EmailTemplateService.Delete(this.EmailTemplateService.GetByID(id));
            return RedirectToAction("Index", "EmailTemplateManagement");
        }
        #endregion

    }
}
